<?php

namespace app\admin\model;

use app\common\constants\MenuConstant;
use app\common\model\TimeModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
// use think\crypt\Crypt;
use app\common\utils\Crypt;

class InsurancePolicy extends TimeModel
{
    protected function getOptions(): array
    {
        return [
            'deleteTime' => 'delete_time',
        ];
    }

    protected $type = [
        // 'start_at_sy'  =>  'timestamp',
        // 'end_at_sy'  =>  'timestamp',
        // 'start_at_jq'  =>  'timestamp',
        // 'end_at_jq'  =>  'timestamp',
        // 'ocr_json_sy' => 'json',
    ];

    public function setStartAtSyAttr($value){
        return empty($value) ? $value : strtotime($value);
    }

    public function getStartAtSyAttr($value){
        return empty($value) ? $value : date('Y-m-d H:i:s', $value);
    }

    public function setEndAtSyAttr($value){
        return empty($value) ? $value : strtotime($value);
    }

    public function getEndAtSyAttr($value){
        return empty($value) ? $value : date('Y-m-d H:i:s', $value);
    }
    public function setStartAtJqAttr($value){
        return empty($value) ? $value : strtotime($value);
    }

    public function getStartAtJqAttr($value){
        return empty($value) ? $value : date('Y-m-d H:i:s', $value);
    }

    public function setEndAtJqAttr($value){
        return empty($value) ? $value : strtotime($value);
    }

    public function getEndAtJqAttr($value){
        return empty($value) ? $value : date('Y-m-d H:i:s', $value);
    }

    public function setIdcardAttr($value){
        return empty($value) ? $value : Crypt::encrypt($value);
    }

    public function getIdcardAttr($value){
        return empty($value) ? $value : Crypt::decrypt($value);
    }

    public function setCreateAtAttr($value){
        if (empty($value)) return $value;
        if (is_numeric($value)) return $value;
        return strtotime($value);
    }

    public function getCreateAtAttr($value){
        return empty($value) ? $value : date('Y-m-d', $value);
    }
    
    public function setCarnoAttr($value){
        return empty($value) ? $value : Crypt::encrypt($value);
    }

    public function getCarnoAttr($value){
        return empty($value) ? $value : Crypt::decrypt($value);
    }
    public function setMobileAttr($value){
        return empty($value) ? $value : Crypt::encrypt($value);
    }

    public function getMobileAttr($value){
        return empty($value) ? $value : Crypt::decrypt($value);
    }

    /**
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws DataNotFoundException
     */
    public static function getList(): array
    {
        $field = 'id,insurer,carno';
        $list = self::field($field)->where([
            ['status', '=', 1],
        ])->select()->toArray();

        return $list;
    }

    public function scopeResy($query, $resy)
    {
    	$query->whereBetween('end_at_sy', $resy)->where('status', 1)->field('*');
    }
    public function scopeRejq($query, $rejq)
    {
    	$query->whereBetween('end_at_jq', $rejq)->where('status', 1)->field('*');
    }

}